<template>
    <div>
        <el-popover placement="bottom"  trigger="hover"  v-if="(listQuery.deleted == 1 && permissionedit()) || listQuery.deleted != 1">
            <div class="popover-content" v-if="functionIndex != 2 && listQuery.deleted != 1">
                <span class="popover-content-title"  v-if="treeData.docType!='02'&&treeData.docType!='05'" @click="openclick">下载</span>
                <!-- <a class="popover-content-title" :href="treeData.docFilePath[0].fileUrl"  v-if="treeData.docType!='02'&&treeData.docType!='05'">下载</a> -->
                <span class="popover-content-title" v-if="!projType&&treeData.docType!='05'" @click="loadCollection">加入收藏夹</span>
                <span class="popover-content-title" @click="loadDetails">详情</span>
                <span class="popover-content-title" v-if="permissionedit()" @click="loadRename">修改</span>
                <span class="popover-content-title" v-if="!projType && permissionMove()" @click="loadMove">移动</span>
                <span class="popover-content-title" v-if="permissionDel()" @click="loadDel">删除</span>
            </div>
            <div class="popover-content" v-if="functionIndex == 2">
                <span class="popover-content-title" @click="removeIsMark">从收藏夹移除</span>
            </div>
            <div class="popover-content" v-if="listQuery.deleted == 1 && permissionedit()">
                <span class="popover-content-title" @click="recoveryFile">恢复至</span>
                <span class="popover-content-title" @click="thoroughFile">彻底删除</span>
            </div>
            <i slot="reference" class="document-left-tree-more el-icon-more"></i>
                <!-- <el-button slot="reference" @click="visibletypechang">手动激活</el-button> -->
        </el-popover>
        <fileEdit ref='fileEdit' :treeData="treeData"></fileEdit>
        <move ref="move"></move>
        <del-content ref='del' :treeData="treeData"></del-content>
        <details-content ref="details" :treeData="treeData"></details-content>
        <recovery-content ref="recovery"></recovery-content>
    </div>

</template>

<script>
import fileEdit from "./fileEdit.vue";
import move from "./move.vue";
import delContent from "./del.vue";
import detailsContent from "./details.vue";
import recoveryContent from "./recovery.vue";
import permissionFunc from "../../../../utils/permission";
import { save, relationDel } from "@/api/favor.js";
import { del } from "@/api/doc.js";
export default {
    data() {
        return {};
    },
    components: {
        fileEdit,
        move,
        delContent,
        detailsContent,
        recoveryContent,
    },
    props: [
        "treeData",
        "listQuery",
        "functionIndex",
        "projType",
        "documentType",
    ],
    methods: {
        openclick(){
           window.open(this.treeData.docFilePath[0].fileUrl, "_blank");
        },
        reminderClick(data) {
            console.log(data);
            this.$router.push({
                path: "/contract/reminder",
                query: { docId: data.docId },
            });
        },
        permissionedit() {
            if (this.treeData.docType == "05") {
                if (
                    permissionFunc.inserted(["doc:form:edit"]) &&
                    permissionFunc.inserted(["contract:form:edit"])
                ) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return permissionFunc.inserted(["doc:form:edit"]);
            }
        },
        permissionMove() {
            if (this.treeData.docType == "05") {
                if (
                    permissionFunc.inserted(["doc:form:move"]) &&
                    permissionFunc.inserted(["contract:form:move"])
                ) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return permissionFunc.inserted(["doc:form:move"]);
            }
        },
        permissionDel() {
            if (this.treeData.docType == "05") {
                if (
                    permissionFunc.inserted(["doc:form:del"]) &&
                    permissionFunc.inserted(["contract:form:del"])
                ) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return permissionFunc.inserted(["doc:form:del"]);
            }
        },

        loadTopping() {
            this.$message({
                showClose: true,
                message: "置顶成功",
                type: "success",
            });
        },
        loadCollection() {
            var _this = this;
            console.log(_this.treeData.docId);
            save({
                docId: _this.treeData.docId,
            }).then((res) => {
                console.log(res);
                _this.$message({
                    showClose: true,
                    message: "收藏成功",
                    type: "success",
                });
            });
        },
        loadRename() {
            var _this = this;
            if (
                this.treeData.docType == "04" ||
                this.treeData.docType == "05" ||
                this.treeData.docType == "06" ||
                this.treeData.docType == "10"
            ) {
                var data = {
                    docType: this.treeData.docType,
                    dirId: this.treeData.dirId,
                    docId: this.treeData.docId,
                };
                if (this.documentType == "合同") {
                    _this.$router.push({
                        path: "/contract/edit",
                        query: {
                            data: JSON.stringify(data),
                        },
                    });
                } else {
                    _this.$router.push({
                        path: "/document/edit",
                        query: {
                            data: JSON.stringify(data),
                        },
                    });
                }
            } else {
                this.$refs.fileEdit.loadFileEdit();
            }
        },
        loadMove() {
            var _this = this;
            var data = {
                data: {
                    dirId: _this.treeData.dirId,
                    dirType: _this.treeData.docType,
                    docId: _this.treeData.docId,
                },
            };
            this.$refs.move.loadMove(data, "file");
        },
        loadDel() {
            this.$refs.del.loadDel("file");
        },
        loadDetails() {
            var _this = this;
            if (this.treeData.docType == "05") {
                var data = {
                    docType: this.treeData.docType,
                    dirId: this.treeData.dirId,
                    docId: this.treeData.docId,
                    mySelf:true
                };
                if (this.documentType == "合同") {
                    this.$router.push({
                        path: "/contract/details",
                        query: {
                            data: JSON.stringify(data),
                        },
                    });
                } else {
                    this.$router.push({
                        path: "/document/details",
                        query: {
                            data: JSON.stringify(data),
                        },
                    });
                }
            } else if (this.treeData.docType == "02") {
                if (_this.treeData.docSubtype == "04") {
                    _this.$refs.details.loadDetails();
                } else {
                    var data = {
                        docType: this.treeData.docType,
                        dirId: this.treeData.dirId,
                        docId: this.treeData.docId,
                        mySelf:true
                    };
                    _this.$router.push({
                        path: "/document/details",
                        query: {
                            data: JSON.stringify(data),
                        },
                    });
                }
            } else {
                this.$refs.details.loadDetails();
            }
        },
        removeIsMark() {
            var _this = this;
            relationDel({
                docId: _this.treeData.docId,
            }).then((res) => {
                _this.updateDocList(_this.treeData, "removeIsMark");
                _this.$message({
                    showClose: true,
                    message: "取消成功",
                    type: "success",
                });
            });
        },
        // 更新文档
        updateDocList(data, type) {
            var docForm = {
                data: data,
                type: type,
            };
            this.$EventBus.$emit("updateDoc", docForm);
        },
        // 彻底删除
        thoroughFile() {
            var _this = this;
            this.$confirm("此操作将永久删除该文件, 是否继续?", "彻底删除", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    del({
                        ids: this.treeData.docId,
                    }).then((res) => {
                        console.log(res);
                        _this.updateDocList(this.treeData, "del");
                        _this.$message({
                            showClose: true,
                            message: "删除成功",
                            type: "success",
                        });
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        // 恢复至
        recoveryFile() {
            var _this = this;
            var data = {
                data: {
                    dirId: _this.treeData.dirId,
                    dirType: _this.treeData.docType,
                    docId: _this.treeData.docId,
                },
            };
            this.$refs.recovery.loadRecovery(data);
        },
    },
};
</script>

<style scoped>
.document-left-tree-more {
    font-size: 12px;
}

.popover-content-title {
    color: #2e405e;
    font-size: 14px;
    display: block;
    width: 100%;
    padding-top: 10px;
    padding-bottom: 10px;
    padding-left: 10px;
    padding-right: 10px;
    /* background-color: aqua; */
}

.popover-content-title:hover {
    background-color: #f5f6f8;
}

/deep/.el-popover {
    padding: 0;
}
</style>
